package me.firstapp.service.shop.impl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.shop.Product;
import me.firstapp.module.shop.ProductSkuRelation;
import me.firstapp.module.shop.ShoppingCart;
import me.firstapp.module.shop.ShoppingCartItem;
import me.firstapp.repository.shop.ProductRepository;
import me.firstapp.repository.shop.ProductSkuRelationRepository;
import me.firstapp.repository.shop.ShoppingCartItemRepository;
import me.firstapp.repository.shop.ShoppingCartRepository;
import me.firstapp.service.shop.ShoppingCartItemService;

@Service
public class ShoppingCartItemServiceImpl implements ShoppingCartItemService {

	@Autowired
	private ShoppingCartRepository shoppingCartRepository;

	@Autowired
	private ShoppingCartItemRepository shoppingCartItemRepository;

	@Autowired
	private ProductRepository productRepository;

	@Autowired
	private ProductSkuRelationRepository productSkuRelationRepository;

	@Override
	public ShoppingCartItem addUpdateShoppingCartItem(Long shoppingCartId, Long productId, Long productSkuRelId,
			Integer counts, Long apiClientId) {
		if (shoppingCartId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "购物车id");
		}
		if (productId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "商品id");
		}
		if (counts == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "新增或更新的数量");
		}
		ShoppingCart shoppingCart = (ShoppingCart) shoppingCartRepository.findById(shoppingCartId, apiClientId);
		if (shoppingCart == null) {
			throw new ServiceException(StatusHouse.SHOPPING_CART_NOT_EXIST);
		}
		Product product = (Product) productRepository.findProductById(productId, apiClientId);
		if (product == null) {
			throw new ServiceException(StatusHouse.PRODUCT_NOT_EXIST);
		}

		ProductSkuRelation productSkuRelation = (ProductSkuRelation) productSkuRelationRepository
				.getById(productSkuRelId);
		if (productSkuRelation == null) {
			throw new ServiceException(StatusHouse.PRDT_SKU_REL_IS_NOT_EXIST);
		}

		ShoppingCartItem shoppingCartItem = shoppingCartItemRepository.findShoppingCartItem(shoppingCartId, productId,
				productSkuRelId);
		if (shoppingCartItem == null) {
			shoppingCartItem = new ShoppingCartItem();
			shoppingCartItem.setShoppingCartId(shoppingCart.getId());
			shoppingCartItem.setProduct(product);
			shoppingCartItem.setProductSkuRelation(productSkuRelation);
			if (counts < 1) {
				throw new ServiceException(StatusHouse.PRODUCT_COUNTS_AT_LEAST_ONE);
			}
			shoppingCartItem.setCounts(counts);
			shoppingCartItem.setValue(counts * productSkuRelation.getPrice());
			shoppingCartItemRepository.save(shoppingCartItem);
		} else {
			if (counts != 0) {
				Integer afterCounts = counts + shoppingCartItem.getCounts();
				if (afterCounts < 1) {
					throw new ServiceException(StatusHouse.PRODUCT_COUNTS_IS_WRONG);
				}
				shoppingCartItem.setCounts(afterCounts);
				shoppingCartItem.setValue(shoppingCartItem.getCounts() * productSkuRelation.getPrice());
				shoppingCartItemRepository.update(shoppingCartItem);
			}
		}

		if (counts != 0) {
			// 更新购物车
			shoppingCart.setCounts(counts + shoppingCart.getCounts());
			Double addValue = counts * productSkuRelation.getPrice();
			BigDecimal bAddValue = new BigDecimal(addValue);
			Double nowValue = shoppingCart.getValue();
			BigDecimal bNowValue = new BigDecimal(nowValue);
			shoppingCart.setValue(bAddValue.add(bNowValue).doubleValue());
			shoppingCartRepository.update(shoppingCart);
		}

		return shoppingCartItem;
	}

	@Override
	public void deleteShoppingCartItems(Long shoppingCartId, String productIds, String productSkuRelIds,
			Long apiClientId) {
		if (shoppingCartId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "购物车id");
		}
		if (StrUtils.isNULL(productIds)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "商品id");
		}
		if (StrUtils.isNULL(productSkuRelIds)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "属性关系id");
		}
		ShoppingCart shoppingCart = (ShoppingCart) shoppingCartRepository.findById(shoppingCartId, apiClientId);
		if (shoppingCart == null) {
			throw new ServiceException(StatusHouse.SHOPPING_CART_NOT_EXIST);
		}
		String[] productIdsArra = productIds.split(",");
		String[] productSkuRelIdsArra = productSkuRelIds.split(",");

		for (int i = 0; i < productIdsArra.length; i++) {
			Long productId = Long.valueOf(productIdsArra[i]);
			Long relId = Long.valueOf(productSkuRelIdsArra[i]);
			ShoppingCartItem shoppingCartItem = shoppingCartItemRepository.findShoppingCartItem(shoppingCartId,
					productId, relId);
			if (shoppingCartItem != null) {
				shoppingCartItemRepository.deleteById(shoppingCartItem.getId());
			}
			Integer totalCounts = shoppingCart.getCounts() - shoppingCartItem.getCounts();
			if (totalCounts < 0) {
				totalCounts = 0;
			}
			BigDecimal bCartValue = new BigDecimal(shoppingCart.getValue());
			BigDecimal bItemValue = new BigDecimal(shoppingCartItem.getValue());
			Double totalValue = bCartValue.subtract(bItemValue).doubleValue();
			if (totalValue < 0) {
				totalValue = 0.00;
			}
			shoppingCart.setCounts(totalCounts);
			shoppingCart.setValue(totalValue);
			shoppingCartRepository.update(shoppingCart);
		}
	}

}
